﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyNhaSach.BusinessLogicLayer
{
    class QuanLyBanHangBLL
    {
        public QuanLyBanHangBLL()
        { 
        
        }
        public DataTable LayHoaDon(String a)
        {
            DataAccessLayer.HoaDonDAO d = new DataAccessLayer.HoaDonDAO();
            return d.LayHoaDon(a);
        }
        public DataTable LayPhieuThu(String a)
        {
            DataAccessLayer.PhieuThuDAO d = new DataAccessLayer.PhieuThuDAO();
            return d.LayPhieuThu(a);
        }
        public int TongNo(String s)
        {

            return 0;
        }
        public DataTable LayTatCaHoaDon()
        {
            DataAccessLayer.HoaDonDAO d = new DataAccessLayer.HoaDonDAO();
            return d.LayTatCaHoaDon();
        }
        public int TienThua(int tientra, int tienno)
        {
            return 0;
        }
        private int demchuso(int n)
        {
            return n < 10 ? 1 : 1 + demchuso(n / 10);
        }
        public string TaoMaHoaDon()
        {            
            string ma="B";
            
            DataAccessLayer.HoaDonDAO d = new DataAccessLayer.HoaDonDAO();
            DataTable dt = d.LayTatCaHoaDon();
            int n = dt.Rows.Count+1;
            for (int i = 0; i < 4 - demchuso(dt.Rows.Count + 1); i++)
            {
                ma += "0";
            }
                ma += n;
            return ma;        
        }
        public string TaoMaPhieuThu()
        {
            string ma;
            DataAccessLayer.PhieuThuDAO d = new DataAccessLayer.PhieuThuDAO();
            DataTable data = d.LayTatCaPhieuThu();
            int n = data.Rows.Count + 1;
            ma = "PT"+n;
            return ma;
        }
        public DataTable LayHoaDonTheoThang(string datetime)
        {
            DataAccessLayer.HoaDonDAO h = new DataAccessLayer.HoaDonDAO();
            return h.LayHoaDonTheoThang(datetime);
        }
        public DataTable LayPhieuThuTheoThang(string datetime)
        {
            DataAccessLayer.PhieuThuDAO p = new DataAccessLayer.PhieuThuDAO();
            return p.LayPhieuThuTheoThang(datetime);
        }
        public DataTable LayHoaDonNoTheoThang(string datetime)
        {
            DataAccessLayer.HoaDonDAO h = new DataAccessLayer.HoaDonDAO();
            return h.LayHoaDonNoTheoThang(datetime);
        }
        public void LuuHoaDon(string mahd,string makh, string ngaylap,string thanhtoan)
        {
            DataAccessLayer.HoaDonDAO h = new DataAccessLayer.HoaDonDAO();
            h.LuuHoaDon(mahd, makh, ngaylap,thanhtoan);
        }
        public DataTable LayCTHD(string mahd)
        {
            DataAccessLayer.HoaDonDAO s = new DataAccessLayer.HoaDonDAO();
            return s.LayCTHD(mahd);
        }
        public int TongTienCuaHoaDon(string mahd)
        {
            DataTable data = LayCTHD(mahd);
            int tien = 0;
            for (int i = 0; i < data.Rows.Count; i++)
            {
                string dongia = data.Rows[i]["DonGia"].ToString();
                string soluong = data.Rows[i]["SoLuong"].ToString();
                int t =(int)float.Parse(dongia);
                int sl = int.Parse(soluong);
                tien += t * sl;
            }
            return tien;
        }
        public void LuuChiTietHoaDon(string mahd, string masach, int soluong, int dongia)
        {
            DataAccessLayer.HoaDonDAO w = new DataAccessLayer.HoaDonDAO();
            w.LuuChiTietHoaDon(mahd,masach,soluong,dongia);
        }
        public void ThemPhieuThu(string mahd, string ngaythu, string sotienthu)
        {
            DataAccessLayer.PhieuThuDAO z=new DataAccessLayer.PhieuThuDAO();
            z.ThemPhieuThu(mahd, ngaythu, sotienthu);
        }
    }
}
